package com.hc.filter;

import com.hc.bean.User;
import com.hc.dao.UserDao;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/go/myOrder*")
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig){
        System.out.println("过滤器启动了");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        System.out.println("nihaoha");
        User user= (User) request.getSession().getAttribute("user");
        System.out.println(user);
        System.out.println(request.getRequestURL());
        System.out.println(request.getRequestURI());
        if(user==null){
            response.sendRedirect("login");
        }
        //无论处理结果如何,过滤器都必须放行
        filterChain.doFilter(servletRequest, servletResponse);
    }


    @Override
    public void destroy() {

    }
}
